@extends('layouts.appManager')

@section('title')连接状态检测@endsection

@section('content')
    <div class="container py-5">
        <div v-for="(item,key) in list" :key="key" class="py-1" v-if="item.status!==-1">
            <el-alert
                  :title="item.title"
                  :type="item.status===0?'info':(item.status===1?'success':'error')"
                  :description="item.msg!=''?item.msg:'检测中...'"
                  show-icon
                  :closable="false"
                  effect="dark">
            </el-alert>
        </div>

        <br>
        <el-button type="primary" v-if="Object.keys(list).length>0" :disabled="list.Bt.status*list.SSH.status*list.DB.status*list.SOAP.status===0" @click="check" >重新检测</el-button>
        <hr>
{{--        <el-input placeholder="请输入密码" v-model="pwd" show-password style="max-width: 200px"></el-input>--}}
        <el-button type="primary" v-if="Object.keys(list).length>0" :disabled="list.DB.status!==1 || list.SOAP.status!==1" @click="resetPwd">重置密码：默认密码必点</el-button>
        <p class="py-2" style="color: #6a737d; font-size: 12px;">
            一次性重置安全密码，包括：1.数据库密码 2.GM账号的密码。
        </p>

        <br>
        服务端核心版本：@{{ version }}

        <div style="margin-top: 15px;" v-if="motd">
            <el-input placeholder="请输入内容" v-model="motd.text" class="input-with-select">
                <template slot="prepend">登录提示（原Motd）：</template>
                <el-button slot="append" icon="el-icon-search" @click="setMotd">保存</el-button>
            </el-input>
        </div>
    </div>
@endsection

@section('script')

    <script>


        var app = new Vue({
            el: '#app',
            data: {
                pwd: '',
                version:'',
                list: {},
                motd:null,
            },
            mounted() {
                this.check();
            },
            methods: {
                init:function(){
                    this.list={
                        Bt:{ title:'宝塔连接状态', status:0,msg:''},
                        SSH:{title:'Linux账号SSH连接状态', status:0,msg:''},
                        DB:{title:'数据库连接状态', status:0,msg:''},
                        SOAP:{title:'GM账号SOAP连接状态', status:0,msg:''},
                    }
                },
                check:function(){
                    this.init();
                    this.statusSSH();
                    this.statusDB();
                    this.statusSOAP();
                    this.statusBt();
                },
                resetPwd:function(){
                    var t=this;
                    post("status/resetPwd",{},res=>{
                        if (res.data[0]===1 && res.data[1]===1){
                            t.$alert('最新的数据库和GM账号密码是：'+res.data[2]+'，请妥善保存。如果重置后，连接状态报错，请联系管理员！', '重置成功', {
                                confirmButtonText: '确定',
                                callback: action => {
                                    t.check();
                                }
                            });
                        }else{
                            t.$alert('密码重置失败，请联系管理员处理', '重置失败', {
                                confirmButtonText: '确定',
                            });
                        }
                        console.log(res);
                    },t,err=>{

                    });
                },
                statusBt:function(){
                    var t=this;
                    post("status/bt",{},res=>{
                        if(res.data!==1&&res.data!==-1){
                            t.list.Bt.status=2;
                            t.list.Bt.msg=res.data;
                        }else{
                            t.list.Bt.status=res.data;
                            t.list.Bt.msg=res.data===1?'连接成功':'无此配置';
                        }
                        console.log(res);
                    },t,err=>{

                    });
                },
                statusSSH:function(){
                    var t=this;
                    post("status/ssh",{},res=>{
                        if(res.data!==1&&res.data!==-1){
                            t.list.SSH.status=2;
                            t.list.SSH.msg='连接失败';
                        }else{
                            t.list.SSH.status=res.data;
                            t.list.SSH.msg=res.data===1?'连接成功':'无此配置';
                        }
                        console.log(res);
                    },t,err=>{

                    });
                },
                statusDB:function(){
                    var t=this;
                    post("status/db",{},res=>{
                        if(res.data!==1&&res.data!==-1){
                            t.list.DB.status=2;
                            t.list.DB.msg='连接失败';

                        }else{
                            t.list.DB.status=res.data;
                            t.list.DB.msg=res.data===1?'连接成功':'无此配置';
                            if (res.data===1)
                                t.getVersion();
                        }
                        console.log(res);
                    },t,err=>{

                    });
                },
                statusSOAP:function(){
                    var t=this;
                    post("status/soap",{},res=>{
                        if(res.data!==1&&res.data!==-1){
                            t.list.SOAP.status=2;
                            t.list.SOAP.msg='连接失败';
                        }else{
                            t.list.SOAP.status=res.data;
                            t.list.SOAP.msg=res.data===1?'连接成功':'无此配置';
                        }
                        console.log(res);
                    },t,err=>{

                    });
                },
                getVersion:function () {
                    var t=this;
                    post("status/getVersion",{},res=>{
                        t.version=res.data
                        if (res.data>20230300)
                            t.getMotd()
                        console.log(res);
                    },t,err=>{

                    });
                },
                getMotd:function () {
                    var t=this;
                    post("status/getMotd",{},res=>{
                        if (res.data.length>0)
                            t.motd=res.data[0];
                    },t,err=>{

                    });
                },
                setMotd:function () {
                    var t=this;
                    post("status/setMotd",{text:t.motd.text},res=>{
                        console.log(res);
                    },t,err=>{

                    });
                },
            }
        })
    </script>
@endsection
